package com.library.workbench.web.controller.admin;

import com.library.commons.constant.Constant;
import com.library.commons.domain.ReturnObject;
import com.library.commons.utils.DateTimeUtils;
import com.library.commons.utils.UUIDUtils;
import com.library.settings.pojo.User;
import com.library.settings.service.UserService;
import com.library.workbench.pojo.Borrow;
import com.library.workbench.service.BorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserWorkbenchController {
    @Autowired
    private UserService userService;

    @Autowired
    private BorrowService borrowService;

    @RequestMapping("/workbench/admin/user/toUserDetail.do")
    public String toUserDetail(){
        return "workbench/admin/user/detail";
    }

    @RequestMapping("/workbench/admin/user/queryUserByActNameSexForPage.do")
    @ResponseBody
    public Object queryUserByActNameSexForPage(String userAccount,String userName,String userSex,Integer pageNo,Integer pageSize){
        // 封装参数
        Map<String,Object> map=new HashMap<>();
        map.put("userAccount",userAccount);
        map.put("userName",userName);
        map.put("userSex",userSex);
        map.put("beginNo",(pageNo-1)*pageSize);
        map.put("pageSize",pageSize);

        // 调用service层方法
        List<User> users = userService.queryUsersByActNameAndSexForPage(map);
        int totalRows = userService.queryUserByActNameAndSexForRows(map);

        Map<String,Object> res=new HashMap<>();
        res.put("users",users);
        res.put("totalRows",totalRows);
        // 返回响应信息
        return res;
    }

    @RequestMapping("/workbench/admin/user/saveUser.do")
    @ResponseBody
    public Object saveUser(User user){
        // 封装参数
        user.setUserId(UUIDUtils.getUUID());
        user.setUserRegdate(DateTimeUtils.formatDateTime(new Date()));
        ReturnObject returnObject=new ReturnObject();
        try {
            // 调用service层方法
            int res = userService.saveUser(user);
            if(res>0){
                returnObject.setCode(Constant.RETURN_SUCCESS);
            }else{
                returnObject.setCode(Constant.RETURN_FAIL);
                returnObject.setMessage("系统繁忙，请稍候再试....");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnObject.setCode(Constant.RETURN_FAIL);
            returnObject.setMessage("系统繁忙，请稍候再试....");
        }
        return returnObject;
    }

    @RequestMapping("/workbench/admin/deleteUserByUserId.do")
    @ResponseBody
    public Object deleteUserByUserId(String userId){
        // 调用service层方法
        // 查询用户的借阅情况
        List<Borrow> borrows = borrowService.queryBorrowByUserId(userId);
        ReturnObject returnObject=new ReturnObject();
        // 如果用户有书未归还，非法删除
        if (borrows!=null&&borrows.size()>0){
            Borrow borrow=null;
            for (int i=0;i<borrows.size();i++){
                borrow=borrows.get(i);
                if (borrow.getReturnTime()==null||"".equals(borrow.getReturnTime())){
                    returnObject.setCode(Constant.RETURN_FAIL);
                    returnObject.setMessage("系统繁忙，请稍候再试....");
                    return returnObject;
                }
            }
        }
        try {
            // 删除用户
            int res = userService.deleteUserByUserId(userId);
            if (res>0){
                returnObject.setCode(Constant.RETURN_SUCCESS);
            }else{
                returnObject.setCode(Constant.RETURN_FAIL);
                returnObject.setMessage("系统繁忙，请稍候再试....");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnObject.setCode(Constant.RETURN_FAIL);
            returnObject.setMessage("系统繁忙，请稍候再试....");
        }
        return returnObject;
    }

    @RequestMapping("/workbench/admin/queryUserInformationByUserId.do")
    @ResponseBody
    public Object queryUserInformationByUserId(String userId){
        // 调用service层方法，查询用户信息
        User user = userService.queryUserByUserId(userId);
        // 返回响应信息
        return user;
    }

    @RequestMapping("/workbench/admin/updateUserInformation.do")
    @ResponseBody
    public Object updateUserInformation(User user){
        ReturnObject returnObject=new ReturnObject();
        try {
            // 调用service层方法
            int res = userService.updateUser(user);
            if (res>0){
                returnObject.setCode(Constant.RETURN_SUCCESS);
            }else{
                returnObject.setCode(Constant.RETURN_FAIL);
                returnObject.setMessage("系统繁忙，请稍候再试....");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnObject.setCode(Constant.RETURN_FAIL);
            returnObject.setMessage("系统繁忙，请稍候再试....");
        }
        return returnObject;
    }
}
